import { _decorator } from 'cc';
import { BaseEntity } from './BaseEntity';
import { GameEvents } from '../constants/GameConstants';
import { EventManager } from '../core/EventManager';
import { SkillSystem } from '../systems/SkillSystem';

const { ccclass, property } = _decorator;

interface PlayerStats {
    hp: number;
    moveSpeed: number;
    damage: number;
    size: number;
}

/**
 * 玩家实体类
 * 
 * 功能：
 * 1. 管理玩家属性和状态
 * 2. 处理玩家输入响应
 * 3. 控制玩家进化系统
 * 4. 管理玩家技能系统
 * 
 * 特点：
 * 1. 属性成长系统
 * 2. 技能解锁机制
 * 3. 进化分支选择
 * 4. 状态同步支持
 */
@ccclass('Player')
export class Player extends BaseEntity {
    @property
    public level: number = 1;

    @property
    private experience: number = 0;

    private evolutionStage: string = 'BASIC';
    private stats: PlayerStats = {
        hp: 100,
        moveSpeed: 5,
        damage: 10,
        size: 1
    };

    @property(SkillSystem)
    private skillSystem: SkillSystem | null = null;

    protected override init() {
        super.init();
        this.initPlayerControls();
        this.updatePlayerSize();
    }

    private initPlayerControls() {
        // 初始化玩家输入控制
    }

    public gainEvolutionPoints(points: number) {
        this.evolutionPoints += points;
        EventManager.instance.emit(GameEvents.EVOLUTION_POINT_CHANGE, this.evolutionPoints);
    }

    public updateStats(newStats: Partial<PlayerStats>) {
        Object.assign(this.stats, newStats);
        this.updatePlayerAttributes();
    }

    private updatePlayerAttributes() {
        // 更新玩家属性
        this.hp = this.stats.hp;
        this.moveSpeed = this.stats.moveSpeed;
        this.updatePlayerSize();
    }

    private updatePlayerSize() {
        // 更新玩家大小
        this.node.setScale(this.stats.size, this.stats.size, this.stats.size);
    }

    public getDamage(): number {
        return this.stats.damage;
    }

    public getStats(): PlayerStats {
        return { ...this.stats };
    }

    public useSkill(skillId: string): boolean {
        if (!this.skillSystem) return false;
        return this.skillSystem.useSkill(skillId);
    }

    public getSkillCooldown(skillId: string): number {
        if (!this.skillSystem) return 0;
        return this.skillSystem.getSkillCooldown(skillId);
    }
} 